/**
 * Created by 7095 on 2021/6/16.
 */

public with sharing class ContractProjectController {

    public List<ContractProduct__c> contractProducts { get; set; }

    public List<ContractProduct__c> contractName { get; set; }

    public List<ContractProject__c> supportType { get; set; }

    public String dataContractProducts { get; set; }

    public String dataContractName { get; set; }//仪器名称

    public String dataSupportType { get; set; }//费用类型

    public String contractId { get; set; }//合同id

    public String saveJson { get; set; }

    public String msg { get; private set; }

    public Boolean isErr { get; private set; }//错误

    public Boolean isSuccess { get; private set; }//是否保存成功

    public ContractProjectController() {
        contractId = System.currentPageReference().getParameters().get('id');
    }

    public void init() {
        List<ProjectBean> tmp = new List<ProjectBean>();
        for (ContractProduct__c cp : [SELECT Id,Name FROM ContractProduct__c WHERE Contract__c = :contractId]) {
            tmp.add(new ProjectBean(cp));
        }
        dataContractName = EncodingUtil.urlEncode(JSON.serialize(tmp), 'UTF-8');
    }

    public void saveProjectData() {
        msg = '';
        try {

            List<Object> saveList = (List<Object>) JSON.deserializeUntyped(EncodingUtil.urlDecode(saveJson, 'UTF-8'));
            Map<String, Object> saveMap = (Map<String, Object>) JSON.deserializeUntyped(EncodingUtil.urlDecode(saveJson, 'UTF-8'));
            List<Object> details = (List<Object>) saveMap.get('details');//获取子表内容
            List<ContractProject__c> cpList = new List<ContractProject__c>();

            System.debug(EncodingUtil.urlDecode(saveJson, 'UTF-8'));

            Id headerId = Utils.getRecordTypeIdByDevNameAndObjName('Header', 'ContractProject__c');
            Id detailId = Utils.getRecordTypeIdByDevNameAndObjName('Body', 'ContractProject__c');

            for (Object obj : saveList) {
                Map<String, Object> mo = (Map<String, Object>) obj;
                ContractProject__c cp = new ContractProject__c();
                cp.Id = DataUtil.getStringValue(mo.get('hid'));
                cp.ProjectSupportName__c = DataUtil.getStringValue(mo.get('cpId'));
                cp.RecordTypeId = headerId;
                cp.StartDate__c = (Date) mo.get('StartDate__c');
                cp.TerminationDate__c = (Date) mo.get('TerminationDate__c');
                cp.EndCustomer__c = DataUtil.getStringValue(mo.get('EndCustomer__c'));//项目最终客户单位(关联Account)
                cp.Number__c = DataUtil.getDecimalValue(mo.get('Number__c'));
                cp.Bond__c = DataUtil.getDecimalValue(mo.get('Bond__c'));
                cp.SupportPrice__c = DataUtil.getDecimalValue(mo.get('SupportPrice__c'));
                cpList.add(cp);

                for (Object ob : details) {
                    Map<String, Object> detail = (Map<String, Object>) ob;
                    ContractProject__c cp1 = new ContractProject__c();
                    cp1.Id = DataUtil.getStringValue(detail.get('bid'));
                    cp1.RecordTypeId = detailId;
                    cp1.Header__c = cp.Id;
                    cp1.Year__c = DataUtil.getStringValue(detail.get('Year__c'));
                    cp1.ProjectCost__c = DataUtil.getDecimalValue(detail.get('ProjectCost__c'));
                    cp1.ReagentTask__c = DataUtil.getDecimalValue(detail.get('ReagentTask__c'));
                }


            }
//
//            SavePoint sp = Database.setSavepoint();
//            try {
//
//                if (indList.size() > 0) update indList;
//                msg = '保存成功!';
//                isSuccess = true;
//
//            } catch (Exception e) {
//                Database.rollback(sp);
//                msg = '保存失败:' + e.getMessage();
//
//            }

        } catch (Exception e) {
            msg = '保存失败:' + e.getMessage();
        }
    }


    public class ProjectBean {

        String hid;//主表id
        String bid;//明细id
        String cpId;//合作仪器id
        String Name;

        public ProjectBean(ContractProduct__c cp) {
            cpId = cp.Id;
            Name = cp.Name;
        }
        public ProjectBean(ContractProject__c cp) {
            cpId = cp.ProjectSupportName__c;
            if (cp.RecordType.developerName == 'Header') hid = cp.Id;
            if (cp.RecordType.developerName == 'Body') bid = cp.Id;
            Name = cp.ProjectSupportName__r.Name ;
        }
    }

}